﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Data;

namespace NReports.Data {
    /// <summary>
    /// XmlConnection handles connections for XML  
    /// </summary>
    public class DataSetConnection : IDbConnection {

        string _connectionString;				// the connection string; of format url=
        string _url;
        string _filename;
        DataSet _dataSet = null;

        bool _isOpen = false;

        public DataSetConnection(string conn) {
            ConnectionString = conn;
        }

        public DataSet DataSet {
            get {
                if (_dataSet == null) {
                    if (!string.IsNullOrEmpty(_url)) {
                        
                        WebRequest request = WebRequest.Create(_url);
                        using (var response = (HttpWebResponse)request.GetResponse()) {
                            using (Stream dataStream = response.GetResponseStream()) {
                                _dataSet = new DataSet();
                                _dataSet.ReadXml(dataStream);

                            }
                        }
                    } else
                        if (!string.IsNullOrEmpty(_filename)) {
                           
                            _dataSet = new DataSet();
                            _dataSet.ReadXml(_filename);
                        }

                }
                return _dataSet;
            }
        }

        internal bool IsOpen {
            get { return _isOpen; }
        }

        #region IDbConnection Members

        public void ChangeDatabase(string databaseName) {
            throw new NotImplementedException("ChangeDatabase method not supported.");
        }

        public IDbTransaction BeginTransaction(System.Data.IsolationLevel il) {
            throw new NotImplementedException("BeginTransaction method not supported.");
        }

        IDbTransaction System.Data.IDbConnection.BeginTransaction() {
            throw new NotImplementedException("BeginTransaction method not supported.");
        }

        public System.Data.ConnectionState State {
            get {
                throw new NotImplementedException("State not implemented");
            }
        }

        string GetParameter(string connectionString, string key) {
            var args = connectionString.Split(';');
            foreach (string a in args) {
                var arg = a.Trim().ToLower();
                var k = key.ToLower() + "=";
                if (a.StartsWith(k)) {
                    return a.Substring(k.Length);
                }
            }
            return null;
        }

        public string ConnectionString {
            get {
                return _connectionString;
            }
            set {
                _url = GetParameter(value, "url");
                _filename = GetParameter(value, "file");
                _connectionString = value;
            }
        }

        public IDbCommand CreateCommand() {
            return new DataSetCommand(this);
        }

        public void Open() {
            if (string.IsNullOrEmpty(_url) && string.IsNullOrEmpty(_filename)) throw new Exception("Invalid connection string");
            _isOpen = true;
        }

        public void Close() {
            _isOpen = false;
        }

        public string Database {
            get {
                return null;			// don't really have a database
            }
        }

        public int ConnectionTimeout {
            get {
                return 0;
            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose() {
            this.Close();
        }

        #endregion
    }
}
